/**
 * 
 */
package com.ideazd.api.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ideazd.api.model.Topic;
import com.ideazd.api.model.TopicPost;
import com.ideazd.api.model.dto.RetDTO;
import com.ideazd.api.service.TopicService;
import com.ideazd.api.tools.SysUtil;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author yzz
 * @create 2016年3月21日 下午3:08:17
 */
@ControllerBind(controllerKey="topic")
public class TopicController extends Controller {
	private static final Logger log = Logger.getLogger(TopicController.class);
	private TopicService topicService = new TopicService();
	
	/**
	 * 话题列表
	 */
	public void getTopicList(){
		String pageIndex = getPara("pageIndex");
		String displayOrder = getPara("displayOrder");
		Map<Object, Object> ret = new HashMap<Object,Object>();
		
		
		if(StrKit.isBlank(pageIndex)||StrKit.isBlank(displayOrder)){
			ret.put("code","-2");
			ret.put("msg","必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		String sort = "followamount";
		
		if(displayOrder.equals("2")){
			sort = "createdate";
		}
		
		try {
			
			Page<Topic> topicPage = topicService.getTopicPage(pageIndex, sort);
			List<Map<Object, Object>> topicList = new ArrayList<Map<Object, Object>>();
			
			for(Topic top : topicPage.getList()){
				Map<Object, Object> topic = new HashMap<Object,Object>();
				topic.put("topId", top.get("topid"));
				topic.put("topTitle", top.get("toptitle"));
				topic.put("listPicture", PropKit.get("server_uri") + top.get("listpicture"));
				topic.put("topPicture", PropKit.get("server_uri") +  SysUtil.validateStr(top.get("toppicture")));
				topic.put("readAmount", top.get("readamount").toString());
				topic.put("followAmount", top.get("followamount").toString());
				topicList.add(topic);
			}
			
			Map<Object, Object> data = new HashMap<Object,Object>();
			data.put("totalPage", topicPage.getTotalPage());
			data.put("topicList", topicList);
			
			ret.put("code","0");
			ret.put("msg","成功");
			ret.put("data", data);
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
		
	}
	
	/**
	 * 话题详情
	 */
	public void getTopicDetail(){
		String topId  = getPara("topId");
		RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(topId)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			Topic topic = Topic.dao.findById(topId);
			ret.put("topId", topic.get("topid"));
			ret.put("topTitle", topic.get("toptitle"));
			ret.put("listPicture", PropKit.get("server_uri") + topic.get("listpicture"));
			ret.put("topPicture", PropKit.get("server_uri") +  SysUtil.validateStr(topic.get("toppicture")));
			ret.put("topContent", topic.get("topContent"));
			ret.put("topSource", topic.get("topSource"));
			ret.put("readAmount", topic.get("readamount").toString());
			ret.put("followAmount", topic.get("followamount").toString());
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		
		renderJson(ret);
		return;
	}
	
	/**
	 * 获取热门话题
	 */
	public void getHotPost(){
		Map<Object, Object> ret = new HashMap<Object,Object>();
		
		try {
			
			List<TopicPost> topicPostList = topicService.getHotPostList();
			List<Map<Object, Object>> dataList = new ArrayList<Map<Object, Object>>();
			
			for(TopicPost post:topicPostList){
				Map<Object, Object> topicPost = new HashMap<Object,Object>();
				topicPost.put("posId", post.get("posid"));
				topicPost.put("nickName	", post.get("nickname"));
				topicPost.put("headPortrait", post.get("headportrait"));
				topicPost.put("posContent", post.get("poscontent"));
				topicPost.put("gender", post.getStr("gender"));
				topicPost.put("createTime", post.get("createtime"));
				dataList.add(topicPost);
			}
			
			ret.put("code","0");
			ret.put("msg","成功");
			ret.put("data", dataList);
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
	}

}
